package com.wf.smartcomm.server.serviceImpl;

import java.math.BigInteger;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.Payment;
import com.wf.smartcomm.server.bean.Repair;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.PaymentService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午5:54:10 
 * 
 */
@Service
@Transactional
public class PaymentServiceImpl implements PaymentService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Payment payment) {
		factory.getCurrentSession().persist(payment);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByUser(User user, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Payment where user = :user");
		query.setParameter("user", user);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Payment pay where pay.user = :user order by pay.updateAt desc");
		query.setParameter("user", user);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void list(Village village,Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select count(*) from t_payment where hid in (select objectId from t_house where vid =:vid)");
		query.setParameter("vid", village.getObjectId());
		BigInteger counts = (BigInteger) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createSQLQuery("select * from t_payment where hid in (select objectId from t_house where vid =:vid) order by updateAt desc").addEntity(Payment.class);
		query.setParameter("vid", village.getObjectId());
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public void update(Payment payment) {
		payment.setUpdateAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().update(payment);
	}

	@Override
	public void delete(Payment payment) {
		factory.getCurrentSession().delete(payment);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Payment get(String objectId) {
		Query query = factory.getCurrentSession().createQuery("from Payment where objectId =:id");
		query.setParameter("id", objectId);
		return (Payment) query.uniqueResult();
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void queryByKey(Village village, String sSearch, Page page) {
		String hql = "select count(*) from t_payment where hid in (select objectId from t_house where vid =:vid and name like :key)";
		Query query = factory.getCurrentSession().createSQLQuery(hql);
		query.setParameter("vid", village.getObjectId());
		query.setParameter("key", "%"+sSearch+"%");
		BigInteger counts = (BigInteger) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		hql = "select * from t_payment where hid in (select objectId from t_house where vid =:vid and name like :key) updateAt desc";
		query = factory.getCurrentSession().createSQLQuery(hql).addEntity(Repair.class);
		query.setParameter("vid", village.getObjectId());
		query.setParameter("key", "%"+sSearch+"%");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Payment getLastByHouse(House house) {
		Query query = factory.getCurrentSession().createQuery("from Payment where house =:house order by createdAt desc limit 1");
		query.setParameter("house", house);
		return (Payment) query.uniqueResult();
	}

}
